<!--车辆分配管理页-->
<template>
    <div style="height: 6vh;background-color:#fff;padding:10px 20px;">
        <span style="line-height:60px;font-size:20px;">车辆分配</span>
    </div>
  <!-- 申请单搜索卡片 -->
    <el-card style="margin:20px;height: 70px;">
        <el-form :inline="true">
            <el-form-item label="工单申请人">
                <el-input placeholder="请输入申请人" style="width:220px;" v-model="search.username" @keydown.enter.prevent="loadApplication"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button @click="resetSearch">重置</el-button>
                <el-button type="primary" @click="loadApplication">查询</el-button>
            </el-form-item>
        </el-form>
    </el-card>

  <!-- 工单信息列表 -->
    <el-card  style="margin:20px;">
        <el-radio-group style="margin-bottom: 15px;" v-model="type">
            <el-radio-button value="50" size="large" @change="loadApplication">待分配</el-radio-button>
            <el-radio-button value="60" size="large" @change="loadApplication">已分配</el-radio-button>
        </el-radio-group>
        <el-table :data="appTableData">
            <el-table-column label="编号" type="index" width="100" align="center"></el-table-column>
            <el-table-column label="工单申请人" prop="username" align="center"></el-table-column>
            <el-table-column label="申请时间" prop="createTime" align="center"></el-table-column>
            <el-table-column label="出发地" prop="departureAddr" align="center"></el-table-column>
            <el-table-column label="目的地" prop="destinationAddr" align="center"></el-table-column>
            <el-table-column label="用车事由" prop="reason" align="center"></el-table-column>
            <el-table-column label="使用开始时间" prop="startTime" align="center"></el-table-column>
            <el-table-column label="使用结束时间" prop="endTime" align="center"></el-table-column>
            <el-table-column label="操作" width="100" align="center">
                <template #default="scope">
                    <!--申请单状态为‘已审核50’显示分配用车；申请单状态为‘分配用车60’显示还车-->
                    <!--加载围栏车辆信息时将申请单id一并传入-->
                    <el-button type="primary" link v-if="type==50" @click="loadGeo(scope.row.id)">分配用车</el-button>
                    <el-button type="primary" link v-if="type==60" @click="back(scope.row.id)">还车</el-button>
                </template>
            </el-table-column>
        </el-table>
    </el-card>

  <!-- 围栏车辆概览弹窗 -->
    <el-dialog title="围栏列表" v-model="dialogVisible" style="padding:40px;" :before-close="handleClose">
        <el-form>
            <el-row :gutter="10">
                <el-col :span="8">
                    <el-form-item label="围栏名称:">
                        <el-input placeholder="请输入" v-model="searchGeofenceForm.name" @keydown.enter="loadGeo"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="围栏状态" >
                        <el-select placeholder="请选择"  v-model="searchGeofenceForm.status" @change="loadGeo">
                            <el-option label="启用" value="1"/>
                            <el-option label="禁用" value="0"/>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="3"></el-col>
                <el-col :span="5">
                    <el-form-item>
                        <el-button-group >
                            <el-button @click="resetSearchGeo">重置</el-button>
                            <el-button type="primary" @click="loadGeo">查询</el-button>
                        </el-button-group>
                    </el-form-item>
                </el-col>
            </el-row>
        </el-form>
        <el-table style="margin:20px 0" :data="geoTableData">
            <el-table-column label="编号" type="index" width="80" align="center" fixed="left"></el-table-column>
            <el-table-column label="围栏名称" prop="name" width="200" align="center"></el-table-column>
            <el-table-column label="车辆总数" prop="totalNum" width="200" align="center"></el-table-column>
            <el-table-column label="可用车辆" prop="availableNum" align="center"></el-table-column>
            <el-table-column label="围栏状态" prop="status" align="center" :formatter="geoStatusFormatter"></el-table-column>
            <el-table-column label="操作" width="100" align="center" fixed="right">
                <template #default="scope">
                    <el-button type="primary" link @click="loadVehicle(scope.row.id)">查看车辆</el-button>
                </template>
            </el-table-column>
        </el-table>
    </el-dialog>

  <!-- 分配车辆弹窗 -->
    <el-dialog title="分配车辆" style="padding:40px;" v-model="vehicleDialogVisible">
        <el-table style="margin:20px 0" :data="vehicleTableData">
            <el-table-column label="编号" type="index" width="80" align="center" fixed="left"></el-table-column>
            <el-table-column label="车辆品牌" prop="brand" width="150" align="center"></el-table-column>
            <el-table-column label="车牌号" prop="license" width="200" align="center"></el-table-column>
            <el-table-column label="车辆类型" prop="type" align="center"></el-table-column>
            <el-table-column label="车辆状态" prop="status" align="center"></el-table-column>
            <el-table-column label="操作" width="100" align="center" fixed="right">
                <template #default="scope">
                    <el-button type="primary" link :disabled="scope.row.status=='2'" @click="distribute(scope.row.id)">分配</el-button>
                </template>
            </el-table-column>
        </el-table>
    </el-dialog>
</template>

<script setup>
import {onMounted, ref} from "vue";
import qs from "qs";
import axios from "axios";
import {ElMessage} from "element-plus";
//用于控制围栏车辆概览弹窗是否可见
const dialogVisible = ref(false);
//用于控制分配车辆弹窗是否可见
const vehicleDialogVisible = ref(false);
//页面加载时执行
onMounted(()=>{
    loadApplication();
});
//围栏状态格式转化
const geoStatusFormatter = (row,column,cellValue,index)=>{
    return cellValue == 1 ? '启用' : '禁用';
}

//定义控制展示申请单的类型变量 待分配-50 已分配-51
const type = ref(50);
//定义变量保存查询条件
const search = ref({
    username:'',
    status:''
})
//定义数组保存申请单表格数据
const appTableData = ref([]);
//定义加载申请单数据的方法
const loadApplication = ()=>{
    search.value.status = type.value
    let data = qs.stringify(search.value);
    axios.get(BASE_URL+'/v1/application/select?'+data)
        .then((res)=>{
          if (res.data.code === 2002){
              appTableData.value = res.data.data;
          } else {
              ElMessage.error(res.data.msg);
          }
        })
}
//重置搜索数据
const resetSearch = ()=>{
    search.value={};
    loadApplication();

}
//定义围栏搜索条件
const searchGeofenceForm = ref({
    name:'',
    search:''
})
//定义数组保存围栏表格数据
const geoTableData = ref([]);
//定义全局变量，保存当前申请单id，分配车辆时使用
let applicationId = ref();
//加载围栏数据，入参为当前行申请单id
const loadGeo = (id)=>{
    applicationId = id;
    dialogVisible.value = true;
    let data = qs.stringify(searchGeofenceForm.value);
    axios.get(BASE_URL+'/v1/geofence/select?'+data)
        .then((res)=>{
            if (res.data.code === 2002){
                console.log(res.data.data)
                geoTableData.value = res.data.data;
            } else {
                ElMessage.error(res.data.msg);
            }
        })
}
//重置围栏搜索条件
const resetSearchGeo = ()=>{
    searchGeofenceForm.value={};
    loadGeo();
}

//定义围栏弹窗关闭方法
const handleClose = ()=>{
    searchGeofenceForm.value={};
    dialogVisible.value = false;
}

//定义车辆表格数组
const vehicleTableData = ref([]);
//获取车辆列表参数
const vehicleParams = ref({
    geofenceId:'',
    geofenceBindStatus:''
})
//获取可用车辆表格列表
const loadVehicle = (id)=>{
    vehicleDialogVisible.value = true;
    let data = qs.stringify(vehicleParams.value);
    axios.get(BASE_URL+'/v1/vehicle/select?geofenceId='+id)
        .then((res)=>{
            if (res.data.code === 2002){
                vehicleTableData.value = res.data.data;
            } else {
                ElMessage.error(res.data.msg);
            }
        })
}

//分配车辆
const distribute = (vehicleId)=>{
    axios.post(BASE_URL+'/v1/application/distribute/'+applicationId+'/'+vehicleId)
        .then((res)=>{
            if (res.data.code === 2002){
                dialogVisible.value=false;
                vehicleDialogVisible.value=false;
                ElMessage.success("分配成功");
                loadApplication();
            } else {
                ElMessage.error(res.data.msg);
            }
        })
}

//还车
const back = (vehicleId)=>{l
    axios.post(BASE_URL+'/v1/application/back/'+applicationId+'/'+vehicleId)
        .then((res)=>{
            if (res.data.code === 2002){
                ElMessage.success("还车成功");
                loadApplication();
            } else {
                ElMessage.error(res.data.msg);
            }
        })
}
</script>

<style scoped>

</style>